home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / vbcc / type_expr.c < prev    next >
C/C++ Source or Header  |  1995-11-18  |  42KB  |  1,025 lines

  1. #include "vbc.h"
  2.  
  3. int alg_opt(np),type_expression2(np);
  4. int test_assignment(struct Typ *,np);
  5. void make_cexpr(np);
  6.  
  7. int dontopt;
  8.  
  9. void insert_const(np p)
  10. /*  Traegt Konstante in entprechendes Feld im Knoten ein        */
  11. {
  12.     if(!p) ierror(0);
  13.     if(!p->ntyp) ierror(0);
  14.     if((p->ntyp->flags&31)==CHAR) p->val.vchar=vchar;
  15.     if((p->ntyp->flags&31)==SHORT) p->val.vshort=vshort;
  16.     if((p->ntyp->flags&31)==INT) p->val.vint=vint;
  17.     if((p->ntyp->flags&31)==LONG) p->val.vlong=vlong;
  18.     if((p->ntyp->flags&31)==(UNSIGNED|CHAR)) p->val.vuchar=vuchar;
  19.     if((p->ntyp->flags&31)==(UNSIGNED|SHORT)) p->val.vushort=vushort;
  20.     if((p->ntyp->flags&31)==(UNSIGNED|INT)) p->val.vuint=vuint;
  21.     if((p->ntyp->flags&31)==(UNSIGNED|LONG)) p->val.vulong=vulong;
  22.     if((p->ntyp->flags&31)==FLOAT) p->val.vfloat=vfloat;
  23.     if((p->ntyp->flags&31)==DOUBLE) p->val.vdouble=vdouble;
  24.     if((p->ntyp->flags&31)==POINTER) p->val.vpointer=vpointer;
  25. }
  26.  
  27. int const_typ(struct Typ *p)
  28. /*  Testet, ob Typ konstant ist oder konstante Elemente enthaelt    */
  29. {
  30.     int i;struct struct_declaration *sd;
  31.     if(p->flags&CONST) return(1);
  32.     if((p->flags&15!=ARRAY)&&(p->flags&UNCOMPLETE))
  33.         if(sd=find_struct((void *)p->exact)){p->flags&=~UNCOMPLETE;p->exact=sd;}
  34.     if((p->flags==STRUCT||p->flags==UNION)&&!(p->flags&UNCOMPLETE))
  35.         for(i=0;i<p->exact->count;i++)
  36.             if(const_typ(p->exact->sl[i].styp)) return(1);
  37.     return(0);
  38. }
  39. void eval_const(union atyps *p,int t)
  40. /*  weist bestimmten globalen Variablen Wert einer CEXPR zu         */
  41. {
  42.     int f=t&15;
  43.     if(!p) ierror(0);
  44.     if(f>=CHAR&&f<=LONG){
  45.         if(!(t&UNSIGNED)){
  46.             if(f==CHAR) vlong=zc2zl(p->vchar);
  47.             if(f==SHORT)vlong=zs2zl(p->vshort);
  48.             if(f==INT)  vlong=zi2zl(p->vint);
  49.             if(f==LONG) vlong=p->vlong;
  50.             vulong=zl2zul(vlong);
  51.             vdouble=zl2zd(vlong);
  52.         }else{
  53.             if(f==CHAR) vulong=zuc2zul(p->vuchar);
  54.             if(f==SHORT)vulong=zus2zul(p->vushort);
  55.             if(f==INT)  vulong=zui2zul(p->vuint);
  56.             if(f==LONG) vulong=p->vulong;
  57.             vlong=zul2zl(vulong);
  58.             vdouble=zul2zd(vulong);
  59.         }
  60.         vpointer=zul2zp(vulong);
  61.     }else{
  62.         if(f==POINTER){
  63.             vulong=zp2zul(p->vpointer);
  64.             vlong=zul2zl(vulong);vdouble=zul2zd(vulong);
  65.         }else{
  66.             if(f==FLOAT) vdouble=zf2zd(p->vfloat); else vdouble=p->vdouble;
  67.             vlong=zd2zl(vdouble);
  68.             vulong=zl2zul(vlong);
  69.         }
  70.     }
  71.     vfloat=zd2zf(vdouble);
  72.     vuchar=zul2zuc(vulong);
  73.     vushort=zul2zus(vulong);
  74.     vuint=zul2zui(vulong);
  75.     vchar=zl2zc(vlong);
  76.     vshort=zl2zs(vlong);
  77.     vint=zl2zi(vlong);
  78. }
  79. struct Typ *arith_typ(struct Typ *a,struct Typ *b)
  80. /*  Erzeugt Typ fuer arithmetische Umwandlung von zwei Operanden    */
  81. {
  82.     int ta,tb;struct Typ *new;
  83.     new=(struct Typ *)mymalloc(TYPS);
  84.     new->next=0;
  85.     ta=a->flags&31;tb=b->flags&31;
  86.     if(ta==DOUBLE||tb==DOUBLE){new->flags=DOUBLE;return(new);}
  87.     if(ta==FLOAT||tb==FLOAT){new->flags=FLOAT;return(new);}
  88.     ta=int_erw(ta);tb=int_erw(tb);
  89.     if(ta==(UNSIGNED|LONG)||tb==(UNSIGNED|LONG)){new->flags=UNSIGNED|LONG;return(new);}
  90.     if((ta==LONG&&tb==(UNSIGNED|INT))||(ta==(UNSIGNED|INT)&&tb==LONG)){
  91.         if(UINT_MAX<=LONG_MAX) new->flags=LONG; else new->flags=UNSIGNED|LONG;
  92.         return(new);
  93.     }
  94.     if(ta==LONG||tb==LONG){new->flags=LONG;return(new);}
  95.     if(ta==(UNSIGNED|INT)||tb==(UNSIGNED|INT)){new->flags=UNSIGNED|INT;return(new);}
  96.     new->flags=INT;
  97.     return(new);
  98. }
  99. int int_erw(int t)
  100. /*  Fuehrt Integer_Erweiterung eines Typen durch                */
  101. {
  102.     if((t&15)>SHORT) return(t);
  103.     if((t&31)==CHAR&&SCHAR_MAX<=INT_MAX) return(INT);
  104.     if((t&31)==SHORT&&SHRT_MAX<=INT_MAX) return(INT);
  105.     if((t&31)==(UNSIGNED|CHAR)&&UCHAR_MAX<=INT_MAX) return(INT);
  106.     if((t&31)==(UNSIGNED|SHORT)&&USHRT_MAX<=INT_MAX) return(INT);
  107.     return(UNSIGNED|INT);
  108. }
  109. int type_expression(np p)
  110. /*  Art Frontend fuer type_expression2(). Setzt dontopt auf 0   */
  111. {
  112.     dontopt=0;
  113.     return(type_expression2(p));
  114. }
  115. int type_expression2(np p)
  116. /*  Erzeugt Typ-Strukturen fuer jeden Knoten des Baumes und     */
  117. /*  liefert eins zurueck, wenn der Baum ok ist, sonst 0         */
  118. /*  Die Berechnung von Konstanten und andere Vereinfachungen    */
  119. /*  sollten vielleicht in eigene Funktion kommen                */
  120. {
  121.     int ok,f=p->flags,mopt=dontopt;
  122.     if(!p){ierror(0);return(1);}
  123. /*    if(p->ntyp) printf("Warnung: ntyp!=0\n");*/
  124.     p->lvalue=0;
  125.     p->sidefx=0;
  126.     ok=1;
  127.     if(f==CALL&&p->left->flags==IDENTIFIER&&!find_var(p->left->identifier,0)){
  128.     /*  implizite Deklaration bei Aufruf einer Funktion     */
  129.         struct struct_declaration *sd;struct Typ *t;
  130.         sd=(struct struct_declaration *)mymalloc(MAXSTRUCTSIZE); /*    hier nur die noetige Groesse nehmen */
  131.         sd->count=0;
  132.         t=(struct Typ *)mymalloc(TYPS);
  133.         t->flags=FUNKT;
  134.         t->exact=add_sd(sd);
  135.         t->next=(struct Typ *)mymalloc(TYPS);
  136.         t->next->next=0;
  137.         t->next->flags=INT;
  138.         add_var(p->left->identifier,t,EXTERN,0);
  139.         free(sd);
  140.     }
  141.     dontopt=0;
  142.     if(f==ADDRESS&&p->left->flags==IDENTIFIER) {p->left->flags|=256;/*puts("&const");*/}
  143.     if(p->left&&p->flags!=ASSIGNADD){ struct struct_declaration *sd;
  144.     /*  bei ASSIGNADD wird der linke Zweig durch den Link bewertet  */
  145.         ok&=type_expression2(p->left); p->sidefx|=p->left->sidefx;
  146.         if(!ok) return(0);
  147.         if((p->left->ntyp->flags&15)!=ARRAY&&(p->left->ntyp->flags&UNCOMPLETE))
  148.             if(sd=find_struct((void *)p->left->ntyp->exact))
  149.                 {p->left->ntyp->flags&=~UNCOMPLETE;p->left->ntyp->exact=sd;}
  150.     }
  151.     if(p->right&&p->right->flags!=MEMBER){ struct struct_declaration *sd;
  152.         if(p->flags==ASSIGNADD) dontopt=1; else dontopt=0;
  153.         ok&=type_expression2(p->right); p->sidefx|=p->right->sidefx;
  154.         if(!ok) return(0);
  155.         if((p->right->ntyp->flags&15)!=ARRAY&&(p->right->ntyp->flags&UNCOMPLETE))
  156.             if(sd=find_struct((void *)p->right->ntyp->exact))
  157.                 {p->right->ntyp->flags&=~UNCOMPLETE;p->right->ntyp->exact=sd;}
  158.     }
  159.  
  160. /*    printf("bearbeite %s\n",ename[p->flags]);*/
  161. /*  Erzeugung von Zeigern aus Arrays                            */
  162. /*  Hier muss noch einiges genauer werden (wie gehoert das?)    */
  163.     if(p->left&&((p->left->ntyp->flags&15)==ARRAY||(p->left->ntyp->flags&15)==FUNKT)){
  164.         if(f!=ADDRESS&&f!=ADDRESSA&&f!=ADDRESSS&&f!=FIRSTELEMENT&&f!=DSTRUCT&&(f<PREINC||f>POSTDEC)&&(f<ASSIGN||f>ASSIGNRSHIFT)){
  165.             np new=(np)mymalloc(NODES);
  166.             if((p->left->ntyp->flags&15)==ARRAY) new->flags=ADDRESSA;
  167.              else new->flags=ADDRESS;
  168.             new->ntyp=0;
  169.             new->left=p->left;
  170.             new->right=0;new->lvalue=0;new->sidefx=0; /* sind sidefx immer 0? */
  171.             p->left=new;
  172.             ok&=type_expression2(p->left);
  173.         }
  174.     }
  175.     if(p->right&&f!=FIRSTELEMENT&&f!=DSTRUCT&&f!=ADDRESSS&&((p->right->ntyp->flags&15)==ARRAY||(p->right->ntyp->flags&15)==FUNKT)){
  176.             np new=(np)mymalloc(NODES);
  177.             if((p->right->ntyp->flags&15)==ARRAY) new->flags=ADDRESSA;
  178.              else new->flags=ADDRESS;
  179.             new->ntyp=0;
  180.             new->left=p->right;
  181.             new->right=0;new->lvalue=0;new->sidefx=0; /* sind sidefx immer 0? */
  182.             p->right=new;
  183.             ok&=type_expression2(p->right);
  184.     }
  185.  
  186.     if(f==IDENTIFIER||f==(IDENTIFIER|256)){
  187.         int ff;struct Var *v;
  188.         v=find_var(p->identifier,0);
  189.         if(v==0){error(82,p->identifier);return(0);}
  190.         ff=v->vtyp->flags&15;
  191.         if((ff>=CHAR&&ff<=DOUBLE)||ff==POINTER||ff==STRUCT||ff==UNION/*||ff==ARRAY*/) p->lvalue=1;
  192.         p->ntyp=clone_typ(v->vtyp);
  193.         /*  arithmetischen const Typ als Konstante behandeln, das muss noch
  194.             deutlich anders werden, bevor man es wirklich so machen kann
  195.         if((p->ntyp->flags&CONST)&&arith(p->ntyp->flags&15)&&v->clist&&!(f&256)){
  196.             p->flags=CEXPR;
  197.             p->val=v->clist->val;
  198.             v->flags|=USEDASSOURCE;
  199.         }*/
  200.         p->flags&=~256;
  201.         if((p->ntyp->flags&15)==ENUM){
  202.         /*  enumerations auch als Konstante (int) behandeln */
  203.             p->flags=CEXPR;
  204.             if(!v->clist) ierror(0);
  205.             p->val=v->clist->val;
  206.             p->ntyp->flags=CONST|INT;
  207.         }
  208.         return(1);
  209.     }
  210.  
  211.     if(f==CEXPR||f==PCEXPR||f==STRING) return(1);
  212.  
  213.     if(f==KOMMA){
  214.         p->ntyp=clone_typ(p->right->ntyp);
  215.         if(f==CEXPR) p->val=p->right->val;
  216.         return(ok);
  217.     }
  218.     if(f==ASSIGN||f==ASSIGNADD){
  219.         if(!p){ierror(0);return(0);}
  220.         if(!p->left){ierror(0);return(0);}
  221.         if(p->left->lvalue==0) {error(86);/*prd(p->left->ntyp);*/return(0);}
  222.         if(const_typ(p->left->ntyp)) {error(87);return(0);}
  223.         if(p->left->ntyp->flags&UNCOMPLETE) {error(88);return(0);}
  224.         if(p->right->ntyp->flags&UNCOMPLETE) {error(88);return(0);}
  225.         p->ntyp=clone_typ(p->left->ntyp);
  226.         p->sidefx=1;
  227.         return(test_assignment(p->left->ntyp,p->right));
  228.     }
  229.     if(f==LOR||f==LAND){
  230.         int a1=-1,a2=-1,m;
  231.         if(f==LAND) m=1; else m=0;
  232.         p->ntyp=(struct Typ *)mymalloc(TYPS);
  233.         p->ntyp->flags=INT;p->ntyp->next=0;
  234.         if(!arith(p->left->ntyp->flags&15)&&(p->left->ntyp->flags&15)!=POINTER)
  235.             {error(89);ok=0;}
  236.         if(!arith(p->right->ntyp->flags&15)&&(p->right->ntyp->flags&15)!=POINTER)
  237.             {error(89);ok=0;}
  238.         if(p->left->flags==CEXPR){
  239.             eval_constn(p->left);
  240.             if(!zdeq(vdouble)||!zuleq(vulong)||!zleq(vlong)) a1=1; else a1=0;
  241.         }
  242.         if(p->right->flags==CEXPR){
  243.             eval_constn(p->right);
  244.             if(!zdeq(vdouble)||!zuleq(vulong)||!zleq(vlong)) a2=1; else a2=0;
  245.         }
  246.         if(a1==1-m||a2==1-m||(a1==m&&a2==m)){
  247.             p->flags=CEXPR;p->sidefx=0;
  248.             if(!p->left->sidefx) {free_expression(p->left);p->left=0;} else p->sidefx=1;
  249.             if(!p->right->sidefx||a1==1-m) {free_expression(p->right);p->right=0;} else p->sidefx=0;
  250.             if(a1==1-m||a2==1-m) {p->val.vint=zl2zi(l2zl((long)(1-m)));}
  251.              else {p->val.vint=zl2zi(l2zl((long)m));}
  252.         }
  253.         return(ok);
  254.     }
  255.     if(f==OR||f==AND||f==XOR){
  256.         if(((p->left->ntyp->flags&15)<CHAR)||((p->left->ntyp->flags&15)>LONG)){error(90);return(0);}
  257.         if(((p->right->ntyp->flags&15)<CHAR)||((p->right->ntyp->flags&15)>LONG)){error(90);return(0);}
  258.         p->ntyp=arith_typ(p->left->ntyp,p->right->ntyp);
  259.         if(!mopt){
  260.             if(!alg_opt(p)) ierror(0);
  261.         }
  262.         return(ok);
  263.     }
  264.     if(f==LESS||f==LESSEQ||f==GREATER||f==GREATEREQ||f==EQUAL||f==INEQUAL){
  265.     /*  hier noch einige Abfragen fuer sichere Entscheidungen einbauen  */
  266.     /*  z.B. unigned/signed-Vergleiche etc.                             */
  267.     /*  die val.vint=0/1-Zuweisungen muessen noch an zint angepasst     */
  268.     /*  werden                                                          */
  269.         zlong s1,s2;zulong u1,u2;zdouble d1,d2;int c=0;
  270.         struct Typ *t;
  271.         if(!arith(p->left->ntyp->flags&15)||!arith(p->right->ntyp->flags&15)){
  272.             if((p->left->ntyp->flags&15)!=POINTER||(p->right->ntyp->flags&15)!=POINTER){
  273.                 if(f!=EQUAL&&f!=INEQUAL){
  274.                     error(92);return(0);
  275.                 }else{
  276.                     if(((p->left->ntyp->flags&15)!=POINTER||p->right->flags!=CEXPR)&&
  277.                        ((p->right->ntyp->flags&15)!=POINTER||p->left->flags!=CEXPR)){
  278.                         error(93);return(0);
  279.                     }else{
  280.                         if(p->left->flags==CEXPR) eval_constn(p->left);
  281.                          else                     eval_constn(p->right);
  282.                         if(vdouble!=0||vlong!=0||vulong!=0)
  283.                             {error(40);return(0);}
  284.                     }
  285.                 }
  286.             }else{
  287.                 if(compare_pointers(p->left->ntyp->next,p->right->ntyp->next,15)){
  288.                 }else{
  289.                     if(f!=EQUAL&&f!=INEQUAL){error(41);}
  290.                     if((p->left->ntyp->next->flags&15)!=VOID&&(p->right->ntyp->next->flags&15)!=VOID)
  291.                         {error(41);}
  292.                 }
  293.             }
  294.         }
  295.         if(p->left->flags==CEXPR){
  296.             eval_constn(p->left);
  297.             d1=vdouble;u1=vulong;s1=vlong;c|=1;
  298.         }
  299.         if(p->right->flags==CEXPR){
  300.             eval_constn(p->right);
  301.             d2=vdouble;u2=vulong;s2=vlong;c|=2;
  302.         }
  303.         p->ntyp=(struct Typ *)mymalloc(TYPS);
  304.         p->ntyp->flags=INT;
  305.         p->ntyp->next=0;
  306.         if(c==3){
  307.             p->flags=CEXPR;
  308.             t=arith_typ(p->left->ntyp,p->right->ntyp);
  309.             if(!p->left->sidefx) {free_expression(p->left);p->left=0;}
  310.             if(!p->right->sidefx) {free_expression(p->right);p->right=0;}
  311.             if((t->flags&15)==FLOAT||(t->flags&15)==DOUBLE){
  312.                 if(f==EQUAL) p->val.vint=zdeqto(d1,d2);
  313.                 if(f==INEQUAL) p->val.vint=!zdeqto(d1,d2);
  314.                 if(f==LESSEQ) p->val.vint=zdleq(d1,d2);
  315.                 if(f==GREATER) p->val.vint=!zdleq(d1,d2);
  316.                 if(f==LESS){
  317.                     if(zdleq(d1,d2)&&!zdeqto(d1,d2)) p->val.vint=1;
  318.                      else p->val.vint=0;
  319.                 }
  320.                 if(f==GREATEREQ){
  321.                     if(!zdleq(d1,d2)||zdeqto(d1,d2)) p->val.vint=1;
  322.                      else p->val.vint=0;
  323.                 }
  324.             }else{
  325.                 if(t->flags&UNSIGNED){
  326.                     if(f==EQUAL) p->val.vint=zuleqto(u1,u2);
  327.                     if(f==INEQUAL) p->val.vint=!zuleqto(u1,u2);
  328.                     if(f==LESSEQ) p->val.vint=zulleq(u1,u2);
  329.                     if(f==GREATER) p->val.vint=!zulleq(u1,u2);
  330.                     if(f==LESS){
  331.                         if(zulleq(u1,u2)&&!zuleqto(u1,u2)) p->val.vint=1;
  332.                          else p->val.vint=0;
  333.                     }
  334.                     if(f==GREATEREQ){
  335.                         if(!zulleq(u1,u2)||zuleqto(u1,u2)) p->val.vint=1;
  336.                          else p->val.vint=0;
  337.                     }
  338.                 }else{
  339.                     if(f==EQUAL) p->val.vint=zleqto(s1,s2);
  340.                     if(f==INEQUAL) p->val.vint=!zleqto(s1,s2);
  341.                     if(f==LESSEQ) p->val.vint=zlleq(s1,s2);
  342.                     if(f==GREATER) p->val.vint=!zlleq(s1,s2);
  343.                     if(f==LESS){
  344.                         if(zlleq(s1,s2)&&!zleqto(s1,s2)) p->val.vint=1;
  345.                          else p->val.vint=0;
  346.                     }
  347.                     if(f==GREATEREQ){
  348.                         if(!zlleq(s1,s2)||zleqto(s1,s2)) p->val.vint=1;
  349.                          else p->val.vint=0;
  350.                     }
  351.                 }
  352.             }
  353.             freetyp(t);
  354.         }
  355.         return(ok);
  356.     }
  357.     if(f==ADD||f==SUB||f==MULT||f==DIV||f==MOD||f==LSHIFT||f==RSHIFT||f==PMULT){
  358.         if(!arith(p->left->ntyp->flags&15)||!arith(p->right->ntyp->flags&15)){
  359.             np new;long sz; int type=0;
  360.             if(f!=ADD&&f!=SUB){error(94);return(0);}
  361.             if((p->left->ntyp->flags&15)==POINTER){
  362.                 if((p->left->ntyp->next->flags&15)==VOID)
  363.                     {error(95);return(0);}
  364.                 if((p->right->ntyp->flags&15)==POINTER){
  365.                     if((p->right->ntyp->next->flags&15)==VOID)
  366.                         {error(95);return(0);}
  367.                     if(!compare_pointers(p->left->ntyp->next,p->right->ntyp->next,15))
  368.                         {error(41);}
  369.                     if(f!=SUB){error(96);return(0);}
  370.                      else {type=3;}
  371.                 }else{
  372.                     if((p->right->ntyp->flags&15)>LONG)
  373.                         {error(97,ename[f]);return(0);}
  374.                     if((p->right->ntyp->flags&15)<CHAR)
  375.                         {error(97,ename[f]);return(0);}
  376.                     if(p->right->flags!=PMULT&&p->right->flags!=PCEXPR){
  377.                         new=(np)mymalloc(NODES);
  378.                         new->flags=PMULT;
  379.                         new->ntyp=0;
  380.                         new->left=p->right;
  381.                         new->right=(np)mymalloc(NODES);
  382.                         new->right->flags=PCEXPR;
  383.                         new->right->left=new->right->right=0;
  384.                         new->right->ntyp=(struct Typ *)mymalloc(TYPS);
  385.                         new->right->ntyp->flags=INT;
  386.                         new->right->ntyp->next=0;
  387.                         sz=szof(p->left->ntyp->next);
  388.                         if(!sz) error(78);
  389.                         new->right->val.vint=zl2zi(l2zl(sz));
  390.                         p->right=new;
  391.                         ok&=type_expression2(p->right);
  392.                     }
  393.                     type=1;
  394.                 }
  395.             }else{
  396.                 np merk;
  397.                 if((p->right->ntyp->flags&15)!=POINTER)
  398.                     {error(98);return(0);}
  399.                 if((p->right->ntyp->next->flags&15)==VOID)
  400.                     {error(95);return(0);}
  401.                 if((p->left->ntyp->flags&15)>LONG)
  402.                     {error(98);return(0);}
  403.                 if((p->left->ntyp->flags&15)<CHAR)
  404.                     {error(98);return(0);}
  405.                 if(p->flags==SUB){error(99);return(0);}
  406.                 if(p->left->flags!=PMULT&&p->left->flags!=PCEXPR){
  407.                     new=(np)mymalloc(NODES);
  408.                     new->flags=PMULT;
  409.                     new->ntyp=0;
  410.                     new->left=p->left;
  411.                     new->right=(np)mymalloc(NODES);
  412.                     new->right->flags=PCEXPR;
  413.                     new->right->left=new->right->right=0;
  414.                     new->right->ntyp=(struct Typ *)mymalloc(TYPS);
  415.                     new->right->ntyp->flags=INT;
  416.                     new->right->ntyp->next=0;
  417.                     sz=szof(p->right->ntyp->next);
  418.                     if(!sz) error(78);
  419.                     new->right->val.vint=zl2zi(l2zl(sz));
  420.                     p->left=new;
  421.                     ok&=type_expression2(p->left);
  422.                 }
  423.                 type=2;
  424.                 merk=p->left;p->left=p->right;p->right=merk;
  425.             }
  426.             if(type==0){ierror(0);return(0);}
  427.             else{
  428.                 if(type==3){
  429.                     p->ntyp=(struct Typ *)mymalloc(TYPS);
  430.                     p->ntyp->next=0;p->ntyp->flags=INT;
  431.                 }else{
  432.                     /*if(type==1)*/ p->ntyp=clone_typ(p->left->ntyp);
  433.                     /* else       p->ntyp=clone_typ(p->right->ntyp);*/
  434.                     /*  Abfrage wegen Vertauschen der Knoten unnoetig   */
  435.                 }
  436.             }
  437.         }else{
  438.             p->ntyp=arith_typ(p->left->ntyp,p->right->ntyp);
  439.             if((p->ntyp->flags&15)>LONG&&(f==MOD||f==LSHIFT||f==RSHIFT))
  440.                 {error(101);ok=0;}
  441.             /*  Typerweiterungen fuer SHIFTS korrigieren    */
  442.             if((f==LSHIFT||f==RSHIFT)&&(p->ntyp->flags&15)==LONG&&(p->left->ntyp->flags&15)<LONG)
  443.                 {p->ntyp->flags&=~15;p->ntyp->flags|=INT;}
  444.         }
  445.         /*  fuegt &a+x zusammen, noch sub und left<->right machen   */
  446.         /*  Bei CEXPR statt PCEXPR auch machen?                     */
  447.         if((p->flags==ADD||p->flags==SUB)){
  448.             np m,c=0,a=0;
  449.             if(p->left->flags==PCEXPR&&p->flags==ADD) c=p->left;
  450.             if(p->right->flags==PCEXPR) c=p->right;
  451.             if(p->left->flags==ADDRESS||p->left->flags==ADDRESSA||p->left->flags==ADDRESSS) a=p->left;
  452.             if(p->right->flags==ADDRESS||p->right->flags==ADDRESSA||p->right->flags==ADDRESSS) a=p->right;
  453.             if(c&&a){
  454.                 m=a->left;
  455.                 /*  kann man das hier so machen oder muss man da mehr testen ?  */
  456.                 while(m->flags==FIRSTELEMENT||m->flags==ADDRESS||m->flags==ADDRESSA||m->flags==ADDRESSS) m=m->left;
  457.                 if(m->flags==IDENTIFIER){
  458.                     if(DEBUG&1) printf("&a+x with %s combined\n",ename[p->left->flags]);
  459.                     eval_const(&c->val,c->ntyp->flags);
  460.                     if(p->flags==ADD) m->val.vlong=zuladd(m->val.vlong,vlong);
  461.                      else m->val.vlong=zlsub(m->val.vlong,vlong);
  462.                     vlong=l2zl((long)szof(m->ntyp));
  463.                     if(!zleq(vlong)&&zulleq(vlong,m->val.vlong)){
  464.                         if(zuleqto(vlong,m->val.vlong))
  465.                             error(79);
  466.                         else
  467.                             error(80);
  468.                     }
  469.                     vlong=l2zl(0L);
  470.                     if(!zleq(m->val.vlong)&&zulleq(m->val.vlong,vlong)) error(80);
  471.                     free_expression(c);
  472.                     if(p->ntyp) freetyp(p->ntyp);
  473.                     *p=*a;
  474.                     free(a);
  475.                     return(type_expression2(p));
  476.                 }
  477.             }
  478.         }
  479.         if(!mopt){
  480.             if(!alg_opt(p)) ierror(0);
  481.         }
  482.         return(ok);
  483.     }
  484.     if(f==CAST){
  485.         if((p->ntyp->flags&15)==VOID) return(ok);
  486.         if((p->left->ntyp->flags&15)==VOID)
  487.             {error(102);return(0);}
  488.         if((!arith(p->ntyp->flags&15)||!arith(p->left->ntyp->flags&15))&&
  489.            ((p->ntyp->flags&15)!=POINTER||(p->left->ntyp->flags&15)!=POINTER)){
  490.             if((p->ntyp->flags&15)==POINTER){
  491.                 if((p->left->ntyp->flags&15)<=LONG){
  492.                     if(sizetab[POINTER]<sizetab[p->left->ntyp->flags&15]){
  493.                         error(103);return(0);
  494.                     }
  495.                 }else{
  496.                     error(104);return(0);
  497.                 }
  498.             }else{
  499.                 if((p->left->ntyp->flags&15)!=POINTER)
  500.                     {error(105);return(0);}
  501.                 if((p->ntyp->flags&15)<=LONG){
  502.                     if(sizetab[p->ntyp->flags&15]<sizetab[POINTER]){
  503.                         error(106);return(0);
  504.                     }
  505.                 }else{
  506.                     error(104);return(0);
  507.                 }
  508.             }
  509.         }
  510.         if(p->left->flags==CEXPR){
  511.             eval_constn(p->left);
  512.             if((p->ntyp->flags&15)==POINTER)
  513.                 if(!zuleq(vulong)||!zleq(vlong)||!zdeq(vdouble))
  514.                     error(81);
  515.             insert_const(p);
  516.             p->flags=CEXPR;
  517.             if(!p->left->sidefx) {free_expression(p->left);p->left=0;}
  518.         }
  519.         return(ok);
  520.     }
  521.     if(f==MINUS||f==KOMPLEMENT||f==NEGATION){
  522.         if(!arith(p->left->ntyp->flags&15)){
  523.             if(f!=NEGATION){error(107);return(0);
  524.             }else{
  525.                 if((p->left->ntyp->flags&15)!=POINTER)
  526.                     {error(108);return(0);}
  527.             }
  528.         }
  529.         if(f==KOMPLEMENT&&(p->left->ntyp->flags&15)>LONG)
  530.             {error(109);return(0);}
  531.         if(f==NEGATION){
  532.             p->ntyp=(struct Typ *)mymalloc(TYPS);
  533.             p->ntyp->next=0;
  534.             p->ntyp->flags=INT;
  535.         }else{
  536.             if(!p->left->ntyp) ierror(0);
  537.             p->ntyp=clone_typ(p->left->ntyp);
  538.             if((p->ntyp->flags&15)<=LONG) p->ntyp->flags=int_erw(p->ntyp->flags);
  539.         }
  540.         if(p->left->flags==CEXPR){
  541.             eval_constn(p->left);
  542.             if(f==KOMPLEMENT){vlong=zlkompl(vlong);vulong=zlkompl(vulong);
  543.                               vint=zl2zi(vlong);vuint=zul2zui(vulong);}
  544.             if(f==MINUS){vdouble=zdneg(vdouble);vfloat=zd2zf(vdouble);
  545.                          vulong=zulneg(vulong);vuint=zul2zui(vulong);
  546.                          vlong=zlneg(vlong);vint=zl2zi(vlong);}
  547.             if(f==NEGATION){if(zdeq(vdouble)&&zuleq(vulong)&&zleq(vlong))
  548.                 vlong=l2zl(1L); else vlong=l2zl(0L);
  549.                 vint=zl2zi(vlong);
  550.             }
  551.             insert_const(p);
  552.             p->flags=CEXPR;
  553.             if(!p->left->sidefx&&p->left) {free_expression(p->left);p->left=0;}
  554.         }
  555.         return(ok);
  556.     }
  557.     if(f==CONTENT){
  558.         if((p->left->ntyp->flags&15)!=POINTER)
  559.             {error(111);return(0);}
  560.         if(type_uncomplete(p->left->ntyp->next))
  561.             {error(112);return(0);}
  562.         p->ntyp=clone_typ(p->left->ntyp->next);
  563.         if((p->ntyp->flags&15)!=ARRAY) p->lvalue=1;
  564.         if(p->left->flags==ADDRESS&&zuleq(p->left->val.vulong)){
  565.         /*  *&x durch x ersetzen                                */
  566.             np merk;
  567.             merk=p->left;
  568.             if(p->ntyp) freetyp(p->ntyp);
  569.             if(p->left->ntyp) freetyp(p->left->ntyp);
  570.             *p=*p->left->left;
  571.             free(merk->left);
  572.             free(merk);
  573.             return(ok);
  574.         }
  575.         /*  *&ax durch firstelement-of(x) ersetzen  */
  576.         if(p->left->flags==ADDRESSA||p->left->flags==ADDRESSS){
  577.             np merk;
  578.             if(DEBUG&1) printf("substitutet * and %s with FIRSTELEMENT\n",ename[p->left->flags]);
  579.             p->flags=FIRSTELEMENT;
  580.             p->lvalue=1;    /*  evtl. hier erst Abfrage ?   */
  581.             merk=p->left;
  582.             p->left=merk->left;
  583.             p->right=merk->right;
  584.             if(merk->ntyp) freetyp(merk->ntyp);
  585.             free(merk);
  586.         }
  587.         return(ok);
  588.     }
  589.     if(f==FIRSTELEMENT){
  590. /*        if((p->left->ntyp->flags&15)!=ARRAY)
  591.             {ierror(0);return(0);}*/
  592.         if((p->left->ntyp->flags&15)==ARRAY) p->ntyp=clone_typ(p->left->ntyp->next);
  593.          else{
  594.             int i,n=-1;
  595.             for(i=0;i<p->left->ntyp->exact->count;i++)
  596.                 if(!strcmp(p->left->ntyp->exact->sl[i].identifier,p->right->identifier)) n=i;
  597.             if(n<0){ierror(0);return(0);}
  598.             p->ntyp=clone_typ(p->left->ntyp->exact->sl[n].styp);
  599.         }
  600.         p->lvalue=1;    /*  hier noch genauer testen ?  */
  601.         return(ok);
  602.     }
  603.     if(f==ADDRESS){
  604.         if((p->left->ntyp->flags&15)!=FUNKT&&(p->left->ntyp->flags&15)!=ARRAY){
  605.             if(!p->left->lvalue){error(115);return(0);}
  606.             if(p->left->flags==IDENTIFIER){
  607.                 struct Var *v;
  608.                 v=find_var(p->left->identifier,0);
  609.                 if(!v){error(116,p->left->identifier);return(0);}
  610.                 if(v->storage_class==REGISTER)
  611.                     {error(117);return(0);}
  612.             }
  613.         }
  614.         p->ntyp=(struct Typ *)mymalloc(TYPS);
  615.         p->ntyp->flags=POINTER;
  616.         p->ntyp->next=clone_typ(p->left->ntyp);
  617.         return(ok);
  618.     }
  619.     if(f==ADDRESSA){
  620.         p->ntyp=clone_typ(p->left->ntyp);
  621.         p->ntyp->flags=POINTER;
  622.         return(ok);
  623.     }
  624.     if(f==ADDRESSS){
  625.         int i,n=-1;
  626.         for(i=0;i<p->left->ntyp->exact->count;i++)
  627.             if(!strcmp(p->left->ntyp->exact->sl[i].identifier,p->right->identifier)) n=i;
  628.         if(n<0){ierror(0);return(0);}
  629.         p->ntyp=(struct Typ *)mymalloc(TYPS);
  630.         p->ntyp->flags=POINTER;
  631.         if(!p->left->ntyp) ierror(0);
  632.         if(!p->left->ntyp->exact) ierror(0);
  633.         if(!p->left->ntyp->exact->sl[n].styp) ierror(0);
  634.         p->ntyp->next=clone_typ(p->left->ntyp->exact->sl[n].styp);
  635.         return(ok);
  636.     }
  637.     if(f==DSTRUCT){
  638.     /*  hier kann man bei unions einiges schneller/einfacher machen */
  639.         int i=0;unsigned long offset=0;struct Typ *t;np new;
  640.         if((p->left->ntyp->flags&15)!=STRUCT&&(p->left->ntyp->flags&15)!=UNION)
  641.             {error(8);return(0);}
  642.         if(type_uncomplete(p->left->ntyp)){error(11,(char *)p->left->ntyp->exact);return(0);}
  643.         if(p->right->flags!=MEMBER)
  644.             {ierror(0);return(0);}
  645.         while(i<p->left->ntyp->exact->count&&strcmp(p->left->ntyp->exact->sl[i].identifier,p->right->identifier)){
  646.             t=p->left->ntyp->exact->sl[i].styp;
  647.             offset=((offset+align[t->flags&15]-1)/align[t->flags&15])*align[t->flags&15];
  648.             offset+=szof(t);
  649.             i++;
  650.         }
  651.         if(i>=p->left->ntyp->exact->count) {error(23,p->right->identifier);return(0);}
  652.  
  653.         t=p->left->ntyp->exact->sl[i].styp;
  654.         offset=((offset+align[t->flags&15]-1)/align[t->flags&15])*align[t->flags&15];
  655.         if((p->left->ntyp->flags&15)==UNION) offset=0;
  656.  
  657.         p->flags=CONTENT;if(p->ntyp) {freetyp(p->ntyp);p->ntyp=0;}
  658.         new=(np)mymalloc(NODES);
  659.         new->flags=ADD;
  660.         new->ntyp=0;
  661.         new->right=(np)mymalloc(NODES);
  662.         new->right->left=new->right->right=0;
  663.         new->right->flags=PCEXPR;
  664.         new->right->ntyp=(struct Typ *)mymalloc(TYPS);
  665.         new->right->ntyp->flags=UNSIGNED|LONG;
  666.         new->right->ntyp->next=0;
  667.         new->right->val.vulong=ul2zul(offset);
  668.         new->left=(np)mymalloc(NODES);
  669.         new->left->flags=ADDRESSS;
  670.         new->left->left=p->left;
  671.         new->left->right=p->right;
  672.         new->left->ntyp=0;
  673.         p->left=new;p->right=0;
  674.  
  675.         return(type_expression2(p));
  676.     }
  677.     if(f==PREINC||f==POSTINC||f==PREDEC||f==POSTDEC){
  678.         if(!p->left->lvalue){error(86);return(0);}
  679.         if(p->left->ntyp->flags&CONST){error(87);return(0);}
  680.         if(!arith(p->left->ntyp->flags&15)){
  681.             if((p->left->ntyp->flags&15)!=POINTER){
  682.                 error(24);return(0);
  683.             }else{
  684.                 if((p->left->ntyp->next->flags&15)==VOID)
  685.                     {error(95);return(0);}
  686.             }
  687.         }
  688.         p->ntyp=clone_typ(p->left->ntyp);
  689.         p->sidefx=1;
  690.         return(ok);
  691.     }
  692.     if(f==CALL){
  693.         struct argument_list *al;int i;
  694.         struct struct_declaration *sd;
  695.         al=p->alist;
  696.         if((p->left->ntyp->flags&15)!=POINTER||(p->left->ntyp->next->flags&15)!=FUNKT)
  697.             {error(26);return(0);}
  698.         sd=p->left->ntyp->next->exact;
  699.         if(!sd){ierror(0);return(0);}
  700.         while(al){
  701.             if(!al->arg) ierror(0);
  702.             if(!type_expression2(al->arg)) return(0);
  703.             al->arg=makepointer(al->arg);
  704.             al=al->next;
  705.         }
  706.         p->sidefx=1;
  707.         p->ntyp=clone_typ(p->left->ntyp->next->next);
  708.         i=0;al=p->alist;
  709.         while(al){
  710.             if(i>=sd->count) return(ok);
  711.             if(!sd->sl[i].styp) return(ok); /* nur Bezeichner, aber kein Typ im Prototype */
  712.             if(!test_assignment(sd->sl[i].styp,al->arg)) return(0);
  713.             i++;al=al->next;
  714.         }
  715.         if(i>=sd->count) return(ok);
  716.         if(sd->sl[i].styp&&(sd->sl[i].styp->flags&15)!=VOID){error(83);/*printf("sd->count=%d\n",sd->count);*/}
  717.         return(ok);
  718.     }
  719.     if(f==COND){
  720.         if(!arith(p->left->ntyp->flags&15)&&(p->left->ntyp->flags&15)!=POINTER){
  721.             error(29);
  722.             return(0);
  723.         }
  724.         if(p->left->flags==CEXPR&&!p->left->sidefx){
  725.             int null;np merk;
  726.             if(DEBUG&1) printf("constant conditional-expression simplified\n");
  727.             eval_constn(p->left);
  728.             if(zleq(vlong)&&zuleq(vulong)&&zdeq(vdouble)) null=1; else null=0;
  729.             free_expression(p->left);
  730.             merk=p->right;
  731.             if(null){
  732.                 free_expression(p->right->left);
  733.                 *p=*p->right->right;
  734.             }else{
  735.                 free_expression(p->right->right);
  736.                 *p=*p->right->left;
  737.             }
  738.             if(merk->ntyp) freetyp(merk->ntyp);
  739.             free(merk);
  740.             return(1);
  741.         }
  742.         p->ntyp=clone_typ(p->right->ntyp);
  743.         return(1);
  744.     }
  745.     if(f==COLON){
  746.         /*  Hier fehlt noch korrekte Behandlung der Typattribute    */
  747.         if(arith(p->left->ntyp->flags&15)&&arith(p->right->ntyp->flags&15)){
  748.             p->ntyp=arith_typ(p->left->ntyp,p->right->ntyp);
  749.             return(1);
  750.         }
  751.         if(compare_pointers(p->left->ntyp,p->right->ntyp,15)){
  752.             p->ntyp=clone_typ(p->left->ntyp);
  753.             return(1);
  754.         }
  755.         if((p->left->ntyp->flags&15)==POINTER&&(p->right->ntyp->flags&15)==POINTER){
  756.             if((p->left->ntyp->next->flags&15)==VOID){
  757.                 p->ntyp=clone_typ(p->left->ntyp);
  758.                 return(1);
  759.             }
  760.             if((p->right->ntyp->next->flags&15)==VOID){
  761.                 p->ntyp=clone_typ(p->right->ntyp);
  762.                 return(1);
  763.             }
  764.         }
  765.         /*  hier fehlt noch pointer : 0     */
  766.         error(31);
  767.         return(0);
  768.     }
  769.     if(f) printf("type_testing fuer diesen Operator (%d) noch nicht implementiert\n",f);
  770.     return(0);
  771. }
  772.  
  773. np makepointer(np p)
  774. /*  Fuehrt automatische Zeigererzeugung fuer Baumwurzel durch   */
  775. /*  Durch mehrmaligen Aufruf von type_expression() ineffizient  */
  776. {
  777.     struct struct_declaration *sd;
  778.     if((p->ntyp->flags&15)!=ARRAY&&(p->ntyp->flags&UNCOMPLETE))
  779.         if(sd=find_struct((void *)p->ntyp->exact))
  780.             {p->ntyp->flags&=~UNCOMPLETE;p->ntyp->exact=sd;}
  781.         else ierror(0);
  782.  
  783.     if((p->ntyp->flags&15)==ARRAY||(p->ntyp->flags&15)==FUNKT){
  784.         np new=(np)mymalloc(NODES);
  785.         if((p->ntyp->flags&15)==ARRAY){
  786.             new->flags=ADDRESSA;
  787.             new->ntyp=clone_typ(p->ntyp);
  788.             new->ntyp->flags=POINTER;
  789. /*            new->ntyp=0;*/
  790.         }else{
  791.             new->flags=ADDRESS;
  792.             new->ntyp=(struct Typ *)mymalloc(TYPS);
  793.             new->ntyp->flags=POINTER;
  794.             new->ntyp->next=clone_typ(p->ntyp);
  795. /*            new->ntyp=0;*/
  796.         }
  797.         new->left=p;
  798.         new->right=0;
  799.         new->lvalue=0;  /* immer korrekt ?  */
  800.         new->sidefx=p->sidefx;
  801. /*        type_expression(new);*/
  802.         return(new);
  803.     }else return(p);
  804. }
  805. int alg_opt(np p)
  806. /*  fuehrt algebraische Vereinfachungen durch                   */
  807. /*  hier noch genau testen, was ANSI-gemaess erlaubt ist etc.   */
  808. /*  v.a. Floating-Point ist evtl. kritisch                      */
  809. {
  810.     int c=0,f,komm,null1,null2,eins1,eins2;np merk;
  811.     zdouble d1,d2;zulong u1,u2;zlong s1,s2;
  812.     f=p->flags;
  813.     /*  kommutativ? */
  814.     if(f==ADD||f==MULT||f==PMULT||(f>=OR&&f<=AND)) komm=1; else komm=0;
  815.     /*  Berechnet Wert, wenn beides Konstanten sind     */
  816.     if(p->left->flags==CEXPR||p->left->flags==PCEXPR){
  817.         eval_constn(p->left);
  818.         d1=vdouble;u1=vulong;s1=vlong;c|=1;
  819.     }
  820.     if(p->right->flags==CEXPR||p->right->flags==PCEXPR){
  821.         eval_constn(p->right);
  822.         d2=vdouble;u2=vulong;s2=vlong;c|=2;
  823.     }
  824.     if(c==3){
  825.         p->flags=CEXPR;
  826.         if(DEBUG&1) printf("did simple constant folding\n");
  827.         if(!p->left->sidefx) {free_expression(p->left);p->left=0;}
  828.         if(!p->right->sidefx) {free_expression(p->right);p->right=0;}
  829.         if(f==AND){
  830.             vulong=zuland(u1,u2);
  831.             vlong=zland(s1,s2);
  832.         }
  833.         if(f==OR){
  834.             vulong=zulor(u1,u2);
  835.             vlong=zlor(s1,s2);
  836.         }
  837.         if(f==XOR){
  838.             vulong=zulxor(u1,u2);
  839.             vlong=zlxor(s1,s2);
  840.         }
  841.         if(f==ADD){
  842.             vulong=zuladd(u1,u2);
  843.             vlong=zladd(s1,s2);
  844.             vdouble=zdadd(d1,d2);
  845.         }
  846.         if(f==SUB){
  847.             vulong=zulsub(u1,u2);
  848.             vlong=zlsub(s1,s2);
  849.             vdouble=zdsub(d1,d2);
  850.         }
  851.         if(f==MULT||f==PMULT){
  852.             vulong=zulmult(u1,u2);
  853.             vlong=zlmult(s1,s2);
  854.             vdouble=zdmult(d1,d2);
  855.             if(f==PMULT) p->flags=PCEXPR;
  856.         }
  857.         if(f==DIV){
  858.             if(zleq(s2)&&zuleq(u2)&&zdeq(d2)){
  859.                 error(84);
  860.                 vlong=l2zl(0L);vulong=zl2zul(vlong);vdouble=zl2zd(vlong);
  861.             }else{
  862.                 if(!zuleq(u2)) vulong=zuldiv(u1,u2);
  863.                 if(!zleq(s2)) vlong=zldiv(s1,s2);
  864.                 if(!zdeq(d2)) vdouble=zddiv(d1,d2);
  865.             }
  866.         }
  867.         if(f==MOD){
  868.             if(zleq(s2)&&zuleq(u2)){
  869.                 error(84);
  870.                 vlong=l2zl(0L);vulong=zl2zul(vlong);
  871.             }else{
  872.                 if(!zuleq(u2)) vulong=zulmod(u1,u2);
  873.                 if(!zleq(s2)) vlong=zlmod(s1,s2);
  874.             }
  875.         }
  876.         if(f==LSHIFT){
  877.             vulong=zullshift(u1,u2);
  878.             vlong=zllshift(s1,s2);
  879.         }
  880.         if(f==RSHIFT){
  881.             vulong=zulrshift(u1,u2);
  882.             vlong=zlrshift(s1,s2);
  883.         }
  884.         vuint=zul2zui(vulong);vint=zl2zi(vlong);vfloat=zd2zf(vdouble);
  885.         insert_const(p);
  886.         return(1);
  887.     }
  888.     /*  Konstanten nach rechts, wenn moeglich       */
  889.     if(c==1&&komm){
  890.         if(DEBUG&1) printf("exchanged commutative constant operand\n");
  891.         merk=p->left;p->left=p->right;p->right=merk;
  892.         c=2;
  893.         d2=d1;u2=u1;s2=s1;
  894.     }
  895.     /*  Vertauscht die Knoten, um Konstanten                */
  896.     /*  besser zusammenzufassen (bei allen Type erlaubt?)   */
  897.     /*  Hier muss noch einiges kontrolliert werden          */
  898.     if(komm&&c==2&&p->flags==p->left->flags){
  899.         if(p->left->right->flags==CEXPR||p->left->right->flags==PCEXPR){
  900.             np merk;
  901.             merk=p->right;p->right=p->left->left;p->left->left=merk;
  902.             if(DEBUG&1) printf("Vertausche Add-Nodes\n");
  903.             return(type_expression(p));
  904.         }
  905.     }
  906.     if(zdeq(d1)&&zuleq(u1)&&zleq(s1)) null1=1; else null1=0;
  907.     if(zdeq(d2)&&zuleq(u2)&&zleq(s2)) null2=1; else null2=0;
  908.     eins1=eins2=0;
  909.     vlong=l2zl(1L);vulong=zl2zul(vlong);vdouble=zl2zd(vlong);
  910.     if(zdeqto(d1,vdouble)&&zuleqto(u1,vulong)&&zleqto(s1,vlong)) eins1=1;
  911.     if(zdeqto(d2,vdouble)&&zuleqto(u2,vulong)&&zleqto(s2,vlong)) eins2=1;
  912.     if(!(p->ntyp->flags&UNSIGNED)){
  913.         vlong=l2zl(-1L);vdouble=zl2zd(vlong);
  914.         if(zdeqto(d1,vdouble)&&zleqto(s1,vlong)) eins1=-1;
  915.         if(zdeqto(d2,vdouble)&&zleqto(s2,vlong)) eins2=-1;
  916.     }
  917.     if(c==2){
  918.         /*  a+0=a-0=a^0=a*1=a/1=a   */
  919.         if(((eins2==1&&(f==MULT||f==PMULT||f==DIV))||(null2&&(f==ADD||f==SUB||f==XOR)))&&!p->right->sidefx){
  920.             if(DEBUG&1){if(f==MULT||f==PMULT||f==DIV) printf("a*/1->a\n"); else printf("a+-^0->a\n");}
  921.             free_expression(p->right);
  922.             merk=p->left;
  923.             *p=*p->left;
  924.             freetyp(merk->ntyp);
  925.             free(merk);
  926.             return(type_expression(p));
  927.         }
  928.         /*  a*0=0   */
  929.         if(null2&&(f==MULT||f==PMULT||f==AND||f==DIV||f==MOD)){
  930.             if(DEBUG&1) printf("a*&/%%0->0\n");
  931.             if(null2&&(f==DIV||f==MOD)) error(84);
  932.             if(p->flags==PMULT) p->flags=PCEXPR; else p->flags=CEXPR;
  933.             /*  hier nur int,long,float,double moeglich, hoffe ich  */
  934.             vlong=l2zl(0L);vulong=zl2zul(vlong);
  935.             vint=zl2zi(vlong);vuint=zul2zui(vulong);
  936.             vdouble=zl2zd(vlong);vfloat=zd2zf(vdouble);
  937.             insert_const(p);
  938.             if(!p->left->sidefx){free_expression(p->left);p->left=0;} else make_cexpr(p->left);
  939.             if(!p->right->sidefx){free_expression(p->right);p->right=0;} else make_cexpr(p->right);
  940. /*            return(type_expression(p));   */
  941.             return(1);
  942.         }
  943.         if(eins2==-1&&(f==MULT||f==PMULT||f==DIV)&&!p->right->sidefx){
  944.             if(DEBUG&1) printf("a*/(-1)->-a\n");
  945.             free_expression(p->right);
  946.             p->right=0;
  947.             p->flags=MINUS;
  948.             return(type_expression(p));
  949.         }
  950.     }
  951.     if(c==1){
  952.         /*  0-a=-a  */
  953.         if(((f==DIV&&eins1==-1)||(f==SUB&&null1))&&!p->left->sidefx){
  954.             if(DEBUG&1){if(f==DIV) printf("-1/a->-a\n"); else printf("0-a->-a\n");}
  955.             free_expression(p->left);
  956.             p->flags=MINUS;
  957.             p->left=p->right;
  958.             p->right=0;
  959.             return(type_expression(p));
  960.         }
  961.         /*  0/a=0   */
  962.         if(f==DIV&&null1){
  963.             if(DEBUG&1) printf("0/a->0\n");
  964.             p->flags=CEXPR;
  965.             /*  fier nur int,long,float,double moeglich, hoffe ich  */
  966.             vlong=l2zl(0L);vulong=zl2zul(vlong);
  967.             vint=zl2zi(vlong);vuint=zul2zui(vulong);
  968.             vdouble=zl2zd(vlong);vfloat=zd2zf(vdouble);
  969.             insert_const(p);
  970.             if(!p->left->sidefx){free_expression(p->left);p->left=0;}else make_cexpr(p->left);
  971.             if(!p->right->sidefx){free_expression(p->right);p->right=0;} else make_cexpr(p->right);
  972.             return(type_expression(p));
  973.         }
  974.     }
  975.     return(1);
  976. }
  977. void make_cexpr(np p)
  978. /*  Macht aus einem Knoten, der durch constant-folding ueberfluessig    */
  979. /*  wurde, eine PCEXPR, sofern er keine Nebenwirkungen von sich aus     */
  980. /*  erzeugt. Hier noch ueberpruefen, ob CEXPR besser waere.             */
  981. /*  Fuehrt rekursiven Abstieg durch. Ist das so korrekt?                */
  982. {
  983.     int f=p->flags;
  984.     if(f!=ASSIGN&&f!=ASSIGNADD&&f!=CALL&&f!=POSTINC&&f!=POSTDEC&&f!=PREINC&&f!=PREDEC){
  985.         p->flags=PCEXPR;
  986.         if(p->left) make_cexpr(p->left);
  987.         if(p->right) make_cexpr(p->right);
  988.     }
  989. }
  990. int test_assignment(struct Typ *zt,np q)
  991. /*  testet, ob q an Typ z zugewiesen werden darf    */
  992. {
  993.     struct Typ *qt=q->ntyp;
  994.     if(arith(zt->flags&15)&&arith(qt->flags&15)) return(1);
  995.     if(((zt->flags&15)==STRUCT&&(qt->flags&15)==STRUCT)||
  996.        ((zt->flags&15)==UNION &&(qt->flags&15)==UNION )){
  997.         if(!compare_pointers(zt,qt,255)){
  998.             error(38);return(0);}
  999.         else return(1);
  1000.     }
  1001.     if((zt->flags&15)==POINTER&&(qt->flags&15)==POINTER){
  1002.         if((zt->next->flags&15)==VOID&&(qt->next->flags&15)!=FUNKT) return(1);
  1003.         if((qt->next->flags&15)==VOID&&(qt->next->flags&15)!=FUNKT) return(1);
  1004.         if(!compare_pointers(zt->next,qt->next,15)){
  1005.             error(85);
  1006.         }else{
  1007.             if((qt->next->flags&CONST)&&!(zt->next->flags&CONST))
  1008.                 error(91);
  1009.             if((qt->next->flags&VOLATILE)&&!(zt->next->flags&VOLATILE))
  1010.                 error(100);
  1011.             if(qt->next->next&&zt->next->next&&!compare_pointers(zt->next->next,qt->next->next,255))
  1012.                 error(110);
  1013.         }
  1014.         return(1);
  1015.     }
  1016.     if((zt->flags&15)==POINTER&&q->flags==CEXPR){
  1017.         eval_constn(q);
  1018.         if(!(zdeq(vdouble)&&zleq(vlong)&&zuleq(vulong)))
  1019.             error(113);
  1020.         return(1);
  1021.     }
  1022.     error(39);return(0);
  1023. }
  1024.  
  1025.